﻿using System;
using System.Data;
using System.Web.UI.WebControls;
using Besture.Bll;
using Besture.Common;
using Besture.Model;
using Besture.Utils;
using Besture.Common.Definition;

namespace Besture.UI.Admin.Service
{
    public partial class RiskTestSet : BasePage
    {
        

        #region 页面加载事件

        /// <summary>
        /// 页面加载
        /// </summary>
        protected override void InitialLoad()
        {
           if(!IsPostBack)
           {
             bindInvestLevel();
			 bindInvestExp();
			 bindInvestHouse();
			 bindInvestFund();
			 bindRiskReason();
			 bindRiskYeild();
			 bindRiskDirection();
			 bindRiskDeepDirection();
			 bindRiskFutureDirection();
			
            if(Request.QueryString["id"] != null && !string.IsNullOrEmpty(Request.QueryString["id"]))
            {
                //编辑编号
                hidEditID.Value = Request.QueryString["id"];                   
                if(!Fun.IsNumber(hidEditID.Value))
                {
                    RedirectErrorUrl("编号错误");
                }               
                InitData(Convert.ToInt32(hidEditID.Value));               
            }           
           }
        }

        /// <summary>
        /// 回发
        /// </summary>
        protected override void PostBackLoad()
        {

        }
        
        /// <summary>
        /// 数据加载
        /// </summary>
        /// <param name="id">编号</param>
        protected void InitData(int id)
        {
            RiskTestDac dac = new RiskTestDac();
            RiskTestModel model = dac.GetModel(id);
            txtMobile.Text = model.Mobile!=null?model.Mobile.ToString():"";
			txtName.Text = model.Name!=null?model.Name.ToString():"";
			ddlInvestLevel.SelectedValue = model.InvestLevel.ToString();
			ddlInvestExp.SelectedValue = model.InvestExp.ToString();
			ddlInvestHouse.SelectedValue = model.InvestHouse.ToString();
			ddlInvestFund.SelectedValue = model.InvestFund.ToString();
			ddlRiskReason.SelectedValue = model.RiskReason.ToString();
			ddlRiskYeild.SelectedValue = model.RiskYeild.ToString();
			var allRiskDirection = model.RiskDirection.Split(',');
			for (int i = 0; i < allRiskDirection.Length; i++)
			{
				for (int j = 0; j < ckbRiskDirection.Items.Count; j++)
				{
					if (allRiskDirection[i] == ckbRiskDirection.Items[j].Value)
						ckbRiskDirection.Items[i].Selected = true;
				}
			}
			var allRiskDeepDirection = model.RiskDeepDirection.Split(',');
			for (int i = 0; i < allRiskDeepDirection.Length; i++)
			{
				for (int j = 0; j < ckbRiskDeepDirection.Items.Count; j++)
				{
					if (allRiskDeepDirection[i] == ckbRiskDeepDirection.Items[j].Value)
						ckbRiskDeepDirection.Items[i].Selected = true;
				}
			}
			var allRiskFutureDirection = model.RiskFutureDirection.Split(',');
			for (int i = 0; i < allRiskFutureDirection.Length; i++)
			{
				for (int j = 0; j < ckbRiskFutureDirection.Items.Count; j++)
				{
					if (allRiskFutureDirection[i] == ckbRiskFutureDirection.Items[j].Value)
						ckbRiskFutureDirection.Items[i].Selected = true;
				}
			}
			txtResult.Text = model.Result!=null?model.Result.ToString():"";
			
        }
        #endregion

        #region 保存

        protected void btnSubmit_Click(object sender, EventArgs eventArgs)
        {
            RiskTestDac dac = new RiskTestDac();
            RiskTestModel model = null;
            string editID = hidEditID.Value;
            //添加
            if(string.IsNullOrEmpty(editID))
            {
                model = new RiskTestModel();
                model.Mobile =txtMobile.Text.Trim();
				model.Name =txtName.Text.Trim();
				model.InvestLevel = ddlInvestLevel.SelectedValue!=""?Convert.ToInt32(ddlInvestLevel.SelectedValue):0;
				model.InvestExp = ddlInvestExp.SelectedValue!=""?Convert.ToInt32(ddlInvestExp.SelectedValue):0;
				model.InvestHouse = ddlInvestHouse.SelectedValue!=""?Convert.ToInt32(ddlInvestHouse.SelectedValue):0;
				model.InvestFund = ddlInvestFund.SelectedValue!=""?Convert.ToInt32(ddlInvestFund.SelectedValue):0;
				model.RiskReason = ddlRiskReason.SelectedValue!=""?Convert.ToInt32(ddlRiskReason.SelectedValue):0;
				model.RiskYeild = ddlRiskYeild.SelectedValue!=""?Convert.ToInt32(ddlRiskYeild.SelectedValue):0;
				string allck = "";
				for (int i = 0; i < ckbRiskDirection.Items.Count; i++)
				{
					if (ckbRiskDirection.Items[i].Selected)
						allck += ckbRiskDirection.Items[i].Value + ",";
				}
				allck = allck.TrimEnd(',');
				model.RiskDirection = allck;
				allck = "";
				for (int i = 0; i < ckbRiskDeepDirection.Items.Count; i++)
				{
					if (ckbRiskDeepDirection.Items[i].Selected)
						allck += ckbRiskDeepDirection.Items[i].Value + ",";
				}
				allck = allck.TrimEnd(',');
				model.RiskDeepDirection = allck;
				allck = "";
				for (int i = 0; i < ckbRiskFutureDirection.Items.Count; i++)
				{
					if (ckbRiskFutureDirection.Items[i].Selected)
						allck += ckbRiskFutureDirection.Items[i].Value + ",";
				}
				allck = allck.TrimEnd(',');
				model.RiskFutureDirection = allck;
				model.Result =txtResult.Text.Trim();
                model.IsDeleted = 0;
                model.CreateTime = DateTime.Now;
                model.UpdateTime = DateTime.Now;
                dac.Add(model);
            }
            //编辑
            else
            {
                model = dac.GetModel(Convert.ToInt32(editID));
                model.Mobile = txtMobile.Text.Trim();
				model.Name = txtName.Text.Trim();
				model.InvestLevel = ddlInvestLevel.SelectedValue!=""?Convert.ToInt32( ddlInvestLevel.SelectedValue):0;
				model.InvestExp = ddlInvestExp.SelectedValue!=""?Convert.ToInt32( ddlInvestExp.SelectedValue):0;
				model.InvestHouse = ddlInvestHouse.SelectedValue!=""?Convert.ToInt32( ddlInvestHouse.SelectedValue):0;
				model.InvestFund = ddlInvestFund.SelectedValue!=""?Convert.ToInt32( ddlInvestFund.SelectedValue):0;
				model.RiskReason = ddlRiskReason.SelectedValue!=""?Convert.ToInt32( ddlRiskReason.SelectedValue):0;
				model.RiskYeild = ddlRiskYeild.SelectedValue!=""?Convert.ToInt32( ddlRiskYeild.SelectedValue):0;
				string allck = "";
				for (int i = 0; i < ckbRiskDirection.Items.Count; i++)
				{
					if (ckbRiskDirection.Items[i].Selected)
						allck += ckbRiskDirection.Items[i].Value + ",";
				}
				allck = allck.TrimEnd(',');
				model.RiskDirection = allck;
				allck = "";
				for (int i = 0; i < ckbRiskDeepDirection.Items.Count; i++)
				{
					if (ckbRiskDeepDirection.Items[i].Selected)
						allck += ckbRiskDeepDirection.Items[i].Value + ",";
				}
				allck = allck.TrimEnd(',');
				model.RiskDeepDirection = allck;
				allck = "";
				for (int i = 0; i < ckbRiskFutureDirection.Items.Count; i++)
				{
					if (ckbRiskFutureDirection.Items[i].Selected)
						allck += ckbRiskFutureDirection.Items[i].Value + ",";
				}
				allck = allck.TrimEnd(',');
				model.RiskFutureDirection = allck;
				model.Result = txtResult.Text.Trim();
                model.UpdateTime = DateTime.Now;
                model.IsDeleted = 0;

                dac.Update(model);
            }
            Fun.Alert("保存成功", "RiskTestList.aspx");
        }
        #endregion

        #region 取消

        public void btnCanel_Click(object sender, EventArgs eventArgs)
        {
            RedirectUrl("RiskTestList.aspx");
        }

        #endregion

        #region 属性值绑定
        /// <summary>
		///绑定投资的了解程度
		/// </summary>
		public void bindInvestLevel()
		{
				DictionaryDac dac = new DictionaryDac();
                DataTable dt = dac.GetDataTableByType(Constants.DICTIONARY_TYPE_您对投资的了解程度);
				this.ddlInvestLevel.Items.Add(new ListItem("请选择",""));
				for(int i=0;i<dt.Rows.Count;i++)
				{
						this.ddlInvestLevel.Items.Add(new ListItem(dt.Rows[i]["Name"].ToString(),dt.Rows[i]["Code"].ToString()));
				}
		}
		/// <summary>
		///绑定投资经验
		/// </summary>
		public void bindInvestExp()
		{
				DictionaryDac dac = new DictionaryDac();
                DataTable dt = dac.GetDataTableByType(Constants.DICTIONARY_TYPE_您的投资经验);
				this.ddlInvestExp.Items.Add(new ListItem("请选择",""));
				for(int i=0;i<dt.Rows.Count;i++)
				{
						this.ddlInvestExp.Items.Add(new ListItem(dt.Rows[i]["Name"].ToString(),dt.Rows[i]["Code"].ToString()));
				}
		}
		/// <summary>
		///绑定置业情况
		/// </summary>
		public void bindInvestHouse()
		{
				DictionaryDac dac = new DictionaryDac();
                DataTable dt = dac.GetDataTableByType(Constants.DICTIONARY_TYPE_您的置业情况);
				this.ddlInvestHouse.Items.Add(new ListItem("请选择",""));
				for(int i=0;i<dt.Rows.Count;i++)
				{
						this.ddlInvestHouse.Items.Add(new ListItem(dt.Rows[i]["Name"].ToString(),dt.Rows[i]["Code"].ToString()));
				}
		}
		/// <summary>
		///绑定可投资金额
		/// </summary>
		public void bindInvestFund()
		{
				DictionaryDac dac = new DictionaryDac();
                DataTable dt = dac.GetDataTableByType(Constants.DICTIONARY_TYPE_您目前可投资金额);
				this.ddlInvestFund.Items.Add(new ListItem("请选择",""));
				for(int i=0;i<dt.Rows.Count;i++)
				{
						this.ddlInvestFund.Items.Add(new ListItem(dt.Rows[i]["Name"].ToString(),dt.Rows[i]["Code"].ToString()));
				}
		}
		/// <summary>
		///绑定投资首选因素
		/// </summary>
		public void bindRiskReason()
		{
				DictionaryDac dac = new DictionaryDac();
                DataTable dt = dac.GetDataTableByType(Constants.DICTIONARY_TYPE_您的投资首选因素);
				this.ddlRiskReason.Items.Add(new ListItem("请选择",""));
				for(int i=0;i<dt.Rows.Count;i++)
				{
						this.ddlRiskReason.Items.Add(new ListItem(dt.Rows[i]["Name"].ToString(),dt.Rows[i]["Code"].ToString()));
				}
		}
		/// <summary>
		///绑定年化收益率
		/// </summary>
		public void bindRiskYeild()
		{
				DictionaryDac dac = new DictionaryDac();
                DataTable dt = dac.GetDataTableByType(Constants.DICTIONARY_TYPE_您期望的年化收益率);
				this.ddlRiskYeild.Items.Add(new ListItem("请选择",""));
				for(int i=0;i<dt.Rows.Count;i++)
				{
						this.ddlRiskYeild.Items.Add(new ListItem(dt.Rows[i]["Name"].ToString(),dt.Rows[i]["Code"].ToString()));
				}
		}
		/// <summary>
		///绑定主要投资方向
		/// </summary>
		public void bindRiskDirection()
		{
				DictionaryDac dac = new DictionaryDac();
                DataTable dt = dac.GetDataTableByType(Constants.DICTIONARY_TYPE_您目前的主要投资方向);
				for(int i=0;i<dt.Rows.Count;i++)
				{
						this.ckbRiskDirection.Items.Add(new ListItem(dt.Rows[i]["Name"].ToString(),dt.Rows[i]["Code"].ToString()));
				}
		}
		/// <summary>
		///绑定了解的投资方向
		/// </summary>
		public void bindRiskDeepDirection()
		{
				DictionaryDac dac = new DictionaryDac();
                DataTable dt = dac.GetDataTableByType(Constants.DICTIONARY_TYPE_您打算深入了解的投资方向);
				for(int i=0;i<dt.Rows.Count;i++)
				{
						this.ckbRiskDeepDirection.Items.Add(new ListItem(dt.Rows[i]["Name"].ToString(),dt.Rows[i]["Code"].ToString()));
				}
		}
		/// <summary>
		///绑定避免的投资方向
		/// </summary>
		public void bindRiskFutureDirection()
		{
				DictionaryDac dac = new DictionaryDac();
                DataTable dt = dac.GetDataTableByType(Constants.DICTIONARY_TYPE_您未来避免的投资方向);
				for(int i=0;i<dt.Rows.Count;i++)
				{
						this.ckbRiskFutureDirection.Items.Add(new ListItem(dt.Rows[i]["Name"].ToString(),dt.Rows[i]["Code"].ToString()));
				}
        }
        #endregion

    }
}
